package com.huixue.server.basic;

import java.io.IOException;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Common {
	private static SqlSessionFactory factory = null;
	private static String resource = "huixue.xml";
	static {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		factory = new SqlSessionFactoryBuilder().build(reader);
	}

	/**
	 * 获取huixue.xml文件中的数据库资源
	 * 
	 * @return factory
	 */
	public static SqlSessionFactory getSqlSessionFactory() {
		return factory;
	}

	/**
	 * 获取当前系统时间－格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @return 日期字符串yyyy-MM-dd HH:mm:ss
	 */
	public static String getSystemDate() {

		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

	}

	/**
	 * 获取输出日志标识
	 * 
	 * @return String
	 */
	public static String getLogFlag(String className) {

		return new StringBuffer().append("\n>>>>>>>>>>: ").append(
				Common.getSystemDate()).append(" -> $").append(className)
				.append("$ -> ").toString();

	}

	/**
	 * 获取全球唯一标识，32位
	 * 
	 * @return String
	 */
	public static String getUUID() {

		String uuid = UUID.randomUUID().toString().replace("-", "");
		return uuid;

	}

	public static String getValueFromParam(String name, String str) {
		String value = null;
		String[] arr = str.split("&");
		for (int i = 0; i < arr.length; i++) {
			if (arr[i].split("=")[0].equals(name)) {
				value = arr[i].split("=")[1];
			}
		}
		return value;
	}
}
